package cn.edu.neusoft.food229.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food229.Commen.Commen;
import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.adapter.CommentsAdapter;
import cn.edu.neusoft.food229.adapter.FoodAdapter;
import cn.edu.neusoft.food229.bean.Comments;
import cn.edu.neusoft.food229.bean.FoodBean;
import cn.edu.neusoft.food229.bean.IsCollectBean;
import cn.edu.neusoft.food229.bean.Result;
import cn.edu.neusoft.food229.listener.CollectListener;
import cn.edu.neusoft.food229.listener.CommentsListener;
import cn.edu.neusoft.food229.listener.FoodDetailListener;
import cn.edu.neusoft.food229.listener.ResultListener;
import cn.edu.neusoft.food229.listener.isCollectListener;
import cn.edu.neusoft.food229.model.CarModel;
import cn.edu.neusoft.food229.model.CmtModel;
import cn.edu.neusoft.food229.model.CollectModel;
import cn.edu.neusoft.food229.model.CommentsModel;
import cn.edu.neusoft.food229.model.Foodmodel;

public class FoodActivity extends AppCompatActivity {
    private TextView tv_name, tv_price, tv_pro;
    private ImageView img;


    private CommentsAdapter adapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private String food_id;
    Context context;
    private ImageButton food_collect,Car;
    private int flag = 1;
    private SharedPreferences sharedPreferences;//简单存储
    private String User_id;
    CollectModel collectmodel = new CollectModel();
    isCollectListener listener = new isCollectListener() {

        @Override
        public void onResponce(IsCollectBean bean, String user_id, String shop_food_id, int flag) {
            if (bean.getCollected().equals("0")) {
                System.out.println("---@#@未收藏_菜谱" + bean.getCollected());
                food_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect1));
            } else {
                System.out.println("---****已收藏_菜谱" + bean.getCollected());
                food_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect));
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        context = FoodActivity.this;
        food_id = getIntent().getStringExtra("food_id");
        sharedPreferences = context.getSharedPreferences("login", MODE_PRIVATE);
        User_id = sharedPreferences.getString("userid", "");
        initViews();
    }

    private void initViews() {
        img = findViewById(R.id.imageView11);
        tv_name = findViewById(R.id.textView25);
        tv_price = findViewById(R.id.textView22);
        tv_pro = findViewById(R.id.textView21);
        Car=findViewById(R.id.shoppingcar);
        food_collect = findViewById(R.id.food_collect);
        food_collect.getBackground().setAlpha(1);
        food_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectmodel.collectFood(User_id, food_id, new CollectListener() {
                    @Override
                    public void onResponce(Result result, String user_id, String shopid) {
                        if (result.getSuccess().equals("0")) {
                            System.out.println("----尚未收藏" + result.getSuccess());
                        } else {

                            collectmodel.collect(User_id, food_id, flag, listener);
                            System.out.println("---收藏" + result.getSuccess());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        System.out.println("---fail");
                    }
                });
            }
        });
        collectmodel.collect(User_id, food_id, flag, listener);

        recyclerView = findViewById(R.id.recyclerView3);
        recyclerView.setHasFixedSize(true);//固定条目高度，提高性能
        CommentsModel model1 = new CommentsModel();
        model1.getResultFoodList(food_id, new FoodDetailListener() {
            @Override
            public void onSuccess(FoodBean bean, String food_id) {
//                详情部分
                Picasso.with(context)
                        .load(Commen.BASEURL + bean.getPic())
                        .placeholder(R.mipmap.ic_launcher)
                        .into(img);
                tv_name.setText(bean.getFoodname());
                //double 转String
                String s = "";
                double price = bean.getPrice();
                s = String.valueOf(price);
                tv_price.setText(s);//---放一个String类型数据
                tv_pro.setText(bean.getIntro());

            }

            @Override
            public void onFail(String msg) {

            }
        });
//-----------列表
        model1.getResult(food_id, new CommentsListener() {
            @Override
            public void onResponce(List<Comments> bean, String food_id) {
                adapter = new CommentsAdapter(context, bean);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFail(String msg) {

            }
        });

Car.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        showmydlg();
    }
});
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void showmydlg() {
        final View view=getLayoutInflater().inflate(R.layout.pinglun4,null);
        AlertDialog dlg=new AlertDialog.Builder(this).setTitle("加入购物车")
                .setIcon(R.mipmap.ic_launcher).setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    final   TextView tv;
                  tv=view.findViewById(R.id.tv_ok);
                        CarModel model=new CarModel();
                        model.addCar(User_id, food_id, 1, new ResultListener() {
                            @Override
                            public void onResponce(Result result) {
                                if(result.getSuccess().equals("0")){
                                System.out.println("-添加失败---"+result.getSuccess());
                            }else{
                                System.out.println("--添加成功-"+result.getSuccess());
                            }
                            }

                            @Override
                            public void onFail(String msg) {

                            }
                        });
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();
        dlg.show();
    }




















}


